package main

import "flag"
import "fmt"
import "strings"
import "shardmaster"
import "time"

var g_ip = flag.Int("port", 9000, "port to listen to")
var g_peers = flag.String("peers", "", "peers' ports (excluding self)")

func main() {
	flag.Parse()
	peers := strings.Split(*g_peers, ",")
	peers = filter(peers, not_ws)
	localport := fmt.Sprintf("127.0.0.1:%v", *g_ip)
	peers = append(peers, localport)
	me := len(peers) - 1
	shardmaster.StartServer(peers, me)
	for {
		time.Sleep(100 * time.Second)
	}
}

func not_ws(s string) bool {
	return strings.TrimSpace(s) != ""
}

func filter(a []string, f func(string) bool) []string {
	ret := make([]string, 0)
	for _, s := range a {
		if f(s) {
			ret = append(ret, s)
		}
	}
	return ret
}
		